#include "friend.h"
#include"protocol.h"
#include"tcpclient.h"


Friend ::Friend(QWidget *parent)
    : QWidget{parent}
{

    //按钮和布局
    m_pShowMsgTE = new QTextEdit;
    m_pFriendListWidget = new QListWidget;
    m_pInputMsgLE = new QLineEdit;

    m_pDelFriendPB = new  QPushButton("删除好友");
    m_pFlushFriendPB = new  QPushButton("刷新好友");
    m_pShowOnlineUserPB = new QPushButton("查看用户");
    m_pSearchUserPB = new  QPushButton("搜索用户");

    m_pMsgSendPB=new QPushButton("发送");
    m_pPrivateChatPB=new QPushButton("私聊");

    //布局
    QVBoxLayout *prightHBL =new QVBoxLayout;
    prightHBL->addWidget(m_pDelFriendPB);
    prightHBL->addWidget(m_pFlushFriendPB);
    prightHBL->addWidget(m_pShowOnlineUserPB);
    prightHBL->addWidget(m_pSearchUserPB);
     prightHBL->addWidget(m_pPrivateChatPB);


    QHBoxLayout *ptopHBL =new QHBoxLayout;
    ptopHBL->addWidget(m_pShowMsgTE);
    ptopHBL->addWidget(m_pFriendListWidget);
    ptopHBL->addLayout(prightHBL);


    QHBoxLayout *pMsgHBL =new QHBoxLayout;
    pMsgHBL->addWidget(m_pInputMsgLE);
    pMsgHBL->addWidget(m_pMsgSendPB);


    m_pOnline=new Online;

    QVBoxLayout *pMain =new QVBoxLayout;
    pMain->addLayout(ptopHBL);
    pMain->addLayout(pMsgHBL);
    pMain->addWidget(m_pOnline);
    m_pOnline->hide();



    //设置布局
    setLayout(pMain);

    //处理 刷新用户在线 的 按钮 信号与槽函数
    connect(m_pShowOnlineUserPB,&QPushButton::clicked,this,&Friend::showOnline);

}

void Friend::showOnline()
{
    if(m_pOnline->isHidden())
    {
        m_pOnline->show();
        //产生pdu
        PDU *pdu=mkPDU(0);
        pdu->uiMsgType=ENUM_MSG_TYPE_ALL_ONLINE_TYPE_REQUEST;
        TcpClient::getInstance().getTcpSocket().write((char *)pdu,pdu->uiPDUlen);
        free(pdu);
        pdu=NULL;
    }
    else
    {
         m_pOnline->hide();
    }
}
